<?php defined('SYSPATH') or die('No direct script access.');

class Validate extends Kohana_Validate
{
    private $jsrules = array(
        'not_empty' => 'required',
        'min_length' => 'minlength',
        'email' => 'email',
    );

    public function get_jsvalidate($jquery_selector, $file = NULL, $translate = TRUE, $params = '')
    {
        return "<script type=\"text/javascript\">
                $(function(){
                
                    $('{$jquery_selector}').validate({"
                    . $this->get_jsvalidate_rules()
                    . ", "
                    . $this->get_jsvalidate_messages($file, $translate)
                    . ($params != '' ? ', ' . $params: '')
                    . "});"
                    . "});"
                    . "</script>";
    }

    public function get_jsvalidate_messages($file = NULL, $translate = TRUE)
    {
        $out = 'messages: { ';

        if ($file !== NULL)
        {
            $j = 0;
            foreach ($this->_rules as $name => $rules)
            {
                if ($j > 0) $out .= ", ";
                $out .= $name . ': { ';
                $i = 0;
                foreach ($rules as $key => $val)
                {
                    if ($i > 0) $out .= ", ";
                    $message = '';
                    
                    if ($message = Kohana::message($file, "{$name}.{$key}")) {}
                    elseif ($message = Kohana::message($file, "{$name}.default")) {}
                    elseif ($message = Kohana::message($file, $key)) {}
                    elseif ($message = Kohana::message('validate', $key)) {}
                    else $message = "{$file}.{$name}.{$key}";
                    
                    $values = null; // TODO

                    if ($translate == TRUE)
                    {
                        if (is_string($translate))
                            $message = __($message, $values, $translate);
			else
                            $message = __($message, $values);
                    }
                    else
                    {
                        if ($values != null)
                            $message = strtr($message, $values);
                    }

                    $message = str_replace('"', '\"', $message);
                    $out .= "{$this->jsrules[$key]}: \"{$message}\"";

                    $i++;
                }

                $out .= ' } ';
                $j++;
            }
        }
        
        $out .= ' } ';
        return $out;
    }

    public function get_jsvalidate_rules()
    {
        $out = 'rules:{ ';
        $j = 0;
        foreach ($this->_rules as $name => $rules)
        {
            if ($j > 0) $out .= ", ";
            $out .= $name . ': { ';
            $i = 0;
            foreach ($rules as $key => $val)
            {
                if ($i > 0) $out .= ", ";

                if ($key == 'not_empty') $out .= 'required: true' ;
                if ($key == 'email') $out .= 'email: true' ;
                if ($key == 'min_length') $out .= 'minlength: ' . $val[0] ;

                $i++;
            }
            $out .= ' } ';
            $j++;
        }
        $out .= ' } ';

        return $out;
    }
}